package org.dromara.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.ErpGoodsInOutInfo;
import org.dromara.erp.mapper.ErpGoodsInOutInfoMapper;
import org.dromara.erp.service.IErpGoodsInOutInfoService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 进出明细Service业务层处理
 *
 * @author Mr Lei
 * @date 2024-06-07
 */
@RequiredArgsConstructor
@Service
public class ErpGoodsInOutInfoServiceImpl implements IErpGoodsInOutInfoService {

    private final ErpGoodsInOutInfoMapper baseMapper;

    /**
     * 查询进出明细
     */
    @Override
    public ErpGoodsInOutInfo queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询进出明细列表
     */
    @Override
    public TableDataInfo<ErpGoodsInOutInfo> queryPageList(ErpGoodsInOutInfo bo, PageQuery pageQuery) {
        Page<ErpGoodsInOutInfo> result = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询进出明细列表
     */
    @Override
    public List<ErpGoodsInOutInfo> queryList(ErpGoodsInOutInfo bo) {
        LambdaQueryWrapper<ErpGoodsInOutInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<ErpGoodsInOutInfo> buildQueryWrapper(ErpGoodsInOutInfo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpGoodsInOutInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getModuleName()), ErpGoodsInOutInfo::getModuleName, bo.getModuleName());
        lqw.like(StringUtils.isNotBlank(bo.getOrderName()), ErpGoodsInOutInfo::getOrderName, bo.getOrderName());
        lqw.eq(bo.getCateId() != null, ErpGoodsInOutInfo::getCateId, bo.getCateId());
        lqw.eq(bo.getGoodsId() != null, ErpGoodsInOutInfo::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getGoodsSpecId() != null, ErpGoodsInOutInfo::getGoodsSpecId, bo.getGoodsSpecId());
        lqw.eq(bo.getStock() != null, ErpGoodsInOutInfo::getStock, bo.getStock());
        lqw.eq(bo.getAmount() != null, ErpGoodsInOutInfo::getAmount, bo.getAmount());
        lqw.eq(bo.getPrice() != null, ErpGoodsInOutInfo::getPrice, bo.getPrice());
        lqw.eq(bo.getMoney() != null, ErpGoodsInOutInfo::getMoney, bo.getMoney());
        return lqw;
    }

    /**
     * 新增进出明细
     */
    @Override
    public Boolean insert(ErpGoodsInOutInfo bo) {
        ErpGoodsInOutInfo add = MapstructUtils.convert(bo, ErpGoodsInOutInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改进出明细
     */
    @Override
    public Boolean update(ErpGoodsInOutInfo bo) {
        ErpGoodsInOutInfo update = MapstructUtils.convert(bo, ErpGoodsInOutInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpGoodsInOutInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除进出明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
